package com.kzj.mls.activity;

import android.os.Bundle;
import android.view.View;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzj.mls.R;
import com.kzj.mls.beans.CouponBean;
import com.kzj.mls.beans.ServiceListBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.OnButtonPressListener;
import com.kzj.mls.recyclerCard.basic.SuperSwipeRefresh;
import com.kzj.mls.recyclerCard.card.Card;
import com.kzj.mls.recyclerCard.card.TextCard;
import com.kzj.mls.recyclerCard.card.other.CouponsCard;
import com.kzj.mls.recyclerCard.card.other.ServiceCard;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by xyg on 2015/11/3.
 */
public class SelectServiceActivity extends GBaseActivity implements SuperSwipeRefresh.OnRefreshReceyerListener {
    private MaterialListView material_listview;
    private SuperSwipeRefresh superSwipeRefresh;
    private TextCard textCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_superswiperefresh_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
        TaskGetServerList();
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
        superSwipeRefresh = (SuperSwipeRefresh) findViewById(R.id.superSwipeRefresh);

        superSwipeRefresh.setPageSize(8);
        superSwipeRefresh.setView(this, material_listview);
        superSwipeRefresh.setOnRefreshReceyerListener(this);
        textCard = new TextCard(this);
        textCard.setText("请勾选您提供的服务项目");
        textCard.setBackground(R.color.color_f0f0f0_grey);
        textCard.setHeight(AbViewUtil.scaleValue(this, 100));
    }

    @Override
    protected void initDatas() {
        setTopTitle("服务设置");
        setLeftImg(View.VISIBLE, R.drawable.back);

    }

    @Override
    protected void fillArrayInListView() {

    }

    protected void fillArrayInListView(List<ServiceListBean> serviceListBeans) {

        material_listview.clear();
        material_listview.add(textCard);
        for (int i = 0; i < serviceListBeans.size(); i++) {
            ServiceCard serviceCard = new ServiceCard(this);
            serviceCard.setServiceListBean(serviceListBeans.get(i));
            serviceCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
                @Override
                public void onButtonPressedListener(View view, Card card) {
                    TaskSetServer(((ServiceCard) card).getServiceListBean().getGoods_id(), (ServiceCard) card);
                }
            });
            material_listview.add(serviceCard);
        }
    }

    private void TaskGetServerList() {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), material_listview, superSwipeRefresh, false,
                Constant.getRootUrl() + Constant.Action.Action_Service_List, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Service_List), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        List<ServiceListBean> serviceListBeans = JSON.parseArray(result.getRetval().toString(), ServiceListBean.class);
                        fillArrayInListView(serviceListBeans);
                        App.getInstance().showToast(result.getMsg());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    private void TaskSetServer(String goods_id, final ServiceCard card) {
        RequestServerManager.getInstance().handleMethod( this, null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_Set_Service, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Set_Service, goods_id), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        card.getServiceListBean().setIsset(!card.getServiceListBean().isIsset());
                        material_listview.getAdapter().notifyDataSetChanged();
                        App.getInstance().showToast("服务设置成功！");
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    @Override
    public void Refresh(MaterialListView recyclerView) {
        TaskGetServerList();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
